package com.wsocket.filter;

import com.wsocket.websocket.WebSocketConfigServer;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.WebSocketImpl;

import javax.servlet.*;
import java.io.IOException;
import java.net.UnknownHostException;

@Slf4j
public class startFilter implements Filter {

    /**
     * 初始化
     */
    @Override
    public void init(FilterConfig fc) throws ServletException {
        this.startWebsocketOnline();
    }

    /**
     * 启动socket服务
     */
    public void startWebsocketOnline() {
        log.info("开始启动websocket");
        WebSocketImpl.DEBUG = false;
        // 端口随便设置，只要不跟现有端口重复就可以
        int port = 8888;
        WebSocketConfigServer s = null;
        try {
            s = new WebSocketConfigServer(port);
            s.start();
        } catch (UnknownHostException e) {
            log.error("启动websocket失败！");
            e.printStackTrace();
        }
        log.info("启动websocket成功！");
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
        // TODO Auto-generated method stub
    }

}
